import { EngineObjsMap } from "@/ESJTypes";
import { ESSceneObject } from "../ESObjects";
import { ESViewer } from "../ESViewer";
import { EngineObject } from "./index";
import { Destroyable } from "xbsj-base";
export class EngineObjectsContext extends Destroyable {
    // registerEngines: { [key: string]: EngineObjsMap } = {};
    private _registerObjsMap: EngineObjsMap = new Map();
    get registerObjsMap() { return this._registerObjsMap }
    set registerObjsMap(value: EngineObjsMap) { this._registerObjsMap = value; }

    register<R extends ESSceneObject, V extends ESViewer>(sceneObjectTypeName: string, objConstructor: new (sceneObject: R, viewer: V) => EngineObject<R, V>) {
        const type = sceneObjectTypeName
        if (this._registerObjsMap.has(sceneObjectTypeName)) {
            console.warn(`${sceneObjectTypeName}的实现类已经被注册了`);
        } else {
            //@ts-ignore
            this._registerObjsMap.set(type, objConstructor);
        }
    }

    getEngineObjConstructor<R extends ESSceneObject, V extends ESViewer>(sceneObjectTypeName: string, viewerType: string) {
        //@ts-ignore
        const map = window.ESSDK_EngineObjs[viewerType] ?? new Map();
        const objConstructor = map.get(sceneObjectTypeName);
        if (!objConstructor) {
            console.warn(`${sceneObjectTypeName}在${viewerType}中未找到对应的实现类,如果该对象有引擎实现类,必须在new ESObjectsManager(ESCesiumViewer,...)时传入引擎视口类参数,否则无法整合该引擎的注册对象！`)
            return undefined;
        } else {
            return objConstructor as unknown as (new (sceneObject: R, viewer: V) => EngineObject<R>);
        }
    }

    createEngineObject<R extends ESSceneObject, V extends ESViewer>(sceneObject: R, viewer: V) {
        const objConstructor = this.getEngineObjConstructor<R, V>(sceneObject.typeName, viewer.typeName);
        if (objConstructor) {
            const engineObj = new objConstructor(sceneObject, viewer);
            engineObj.createdEvent.emit();
            return engineObj;
        } else {
            const engineObj = new EngineObject(sceneObject, viewer);
            engineObj.createdEvent.emit();
            return engineObj;
        }
    }
}
